package com.pzb.page.service.impl;

import java.util.List;

import com.pzb.common.utils.StringUtils;
import com.pzb.goods.domain.GdGoods;
import com.pzb.goods.service.IGdGoodsService;
import com.pzb.market.domain.MkGroupbuyingGoods;
import com.pzb.market.domain.MkPanicbuyingGoods;
import com.pzb.market.service.IMkGroupbuyingGoodsService;
import com.pzb.market.service.IMkPanicbuyingGoodsService;
import com.pzb.page.domain.*;
import com.pzb.page.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.page.mapper.PgPageItemMapper;

/**
 * 页面模块Service业务层处理
 * 
 * @author pzb
 * @date 2021-09-25
 */
@Service
public class PgPageItemServiceImpl implements IPgPageItemService 
{
    @Autowired
    private PgPageItemMapper pgPageItemMapper;
    @Autowired
    private IPgPageItemScrollposterService scrollposterService;
    @Autowired
    private IPgPageItemHotspotareaService hotspotareaService;
    @Autowired
    private IPgPageItemNoticeService noticeService;
    @Autowired
    private IPgPageItemEjectService ejectService;
    @Autowired
    private IPgPageItemGoodsService itemGoodsService;
    @Autowired
    private IGdGoodsService gdGoodsService;
    @Autowired
    private IPgPageItemVideoService videoService;
    @Autowired
    private IPgPageItemGroupbuyingService groupbuyingService;
    @Autowired
    private IPgPageItemPanicbuyingService panicbuyingService;
    @Autowired
    private IMkGroupbuyingGoodsService groupbuyingGoodsService;
    @Autowired
    private IMkPanicbuyingGoodsService panicbuyingGoodsService;
    @Autowired
    private IPgPageItemPictureService pictureService;
    /**
     * 查询页面模块
     * 
     * @param id 页面模块主键
     * @return 页面模块
     */
    @Override
    public PgPageItem selectPgPageItemById(Long id)
    {
        return pgPageItemMapper.selectPgPageItemById(id);
    }

    /**
     * 查询页面模块列表
     * 
     * @param pgPageItem 页面模块
     * @return 页面模块
     */
    @Override
    public List<PgPageItem> selectPgPageItemList(PgPageItem pgPageItem)
    {
        List<PgPageItem> itemList = pgPageItemMapper.selectPgPageItemList(pgPageItem);
        for (PgPageItem item : itemList) {
            if (item.getItemCode().intValue() == 1) {
                PgPageItemScrollposter q = new PgPageItemScrollposter();
                q.setItemId(item.getId());
                List<PgPageItemScrollposter> scrollposterList = scrollposterService.selectPgPageItemScrollposterList(q);
                item.setScrollposterList(scrollposterList);
            }else if(item.getItemCode().intValue() == 2){
                PgPageItemEject eject =ejectService.selectPgPageItemEjectByItemId(item.getId());
                item.setEject(eject);
            }else if(item.getItemCode().intValue() == 3){
                PgPageItemNotice notice =noticeService.selectPgPageItemNoticeByItemId(item.getId());
                item.setNotice(notice);
            }else if(item.getItemCode().intValue() == 4){
                PgPageItemHotspotarea q=new PgPageItemHotspotarea();
                q.setItemId(item.getId());
                List<PgPageItemHotspotarea> hotspotareaList=hotspotareaService.selectPgPageItemHotspotareaList(q);
                item.setHotspotareaList(hotspotareaList);
            }else if(item.getItemCode().intValue() == 5){ //商品展示区
                PgPageItemGoods itemGoods =itemGoodsService.selectPgPageItemGoodsByItemId(item.getId());
                item.setItemGoods(itemGoods);
                List<GdGoods> itemGoodsList = gdGoodsService.selectPgPageItemGoodsListByItemId(item.getId());
                item.setItemGoodsList(itemGoodsList);
            }else if(item.getItemCode().intValue() == 6){ //视频展示区
                PgPageItemVideo video =videoService.selectPgPageItemVideoByItemId(item.getId());
                item.setVideo(video);
            }else if(item.getItemCode().intValue() == 7){ //拼团
                PgPageItemGroupbuying groupbuy = groupbuyingService.selectPgPageItemGroupbuyingByItemId(item.getId());
                if(groupbuy!=null && StringUtils.isNotBlank(groupbuy.getLinkAddress())){
                    MkGroupbuyingGoods q = new MkGroupbuyingGoods();
                    q.setGroupId(Long.parseLong(groupbuy.getLinkAddress()));
                    q.setIsShow("0");
                    List<MkGroupbuyingGoods> goodsList = groupbuyingGoodsService.selectMkGroupbuyingGoodsList(q);
                    groupbuy.setGroupbuyGoodsList(goodsList);
                }
                item.setGroupbuy(groupbuy);
            }else if(item.getItemCode().intValue() == 8){ //限时抢购
                PgPageItemPanicbuying panicbuy = panicbuyingService.selectPgPageItemPanicbuyingByItemId(item.getId());
                if(panicbuy!=null && StringUtils.isNotBlank(panicbuy.getLinkAddress())){
                    MkPanicbuyingGoods q =new MkPanicbuyingGoods();
                    q.setPanicId(Long.parseLong(panicbuy.getLinkAddress()));
                    q.setIsShow("0");
                    List<MkPanicbuyingGoods> goodsList=panicbuyingGoodsService.selectMkPanicbuyingGoodsList(q);
                    panicbuy.setPanicbuyGoodsList(goodsList);
                }
                item.setPanicbuy(panicbuy);
            }else if(item.getItemCode().intValue() == 9){ //单图展示
                PgPageItemPicture picture =pictureService.selectPgPageItemPictureByItemId(item.getId());
                item.setPicture(picture);
            }
        }
        return itemList;
    }

    /**
     * 新增页面模块
     * 
     * @param pgPageItem 页面模块
     * @return 结果
     */
    @Override
    public int insertPgPageItem(PgPageItem pgPageItem)
    {
        return pgPageItemMapper.insertPgPageItem(pgPageItem);
    }

    /**
     * 修改页面模块
     * 
     * @param pgPageItem 页面模块
     * @return 结果
     */
    @Override
    public int updatePgPageItem(PgPageItem pgPageItem)
    {
        if(pgPageItem.getItemCode().intValue() == 1 ){ //轮播海报
            //删除原有
            scrollposterService.deleteScrollposterByItemId(pgPageItem.getId());
            //新增
            List<PgPageItemScrollposter> scrollposterList=pgPageItem.getScrollposterList();
            if (scrollposterList.size()>0) {
                for (int i = 0; i <  scrollposterList.size() ; i++) {
                    scrollposterList.get(i).setSort(i+1);
                }
                scrollposterService.batchSaveScrollposter(scrollposterList);
            }
        }else if(pgPageItem.getItemCode().intValue() == 2 ){ //弹出图层
            //删除原有
            ejectService.deletePgPageItemEjectByItemId(pgPageItem.getId());
            //新增
            PgPageItemEject eject=pgPageItem.getEject();
            if (eject!=null) {
                ejectService.insertPgPageItemEject(eject);
            }
        }else if(pgPageItem.getItemCode().intValue() == 3 ){ //公告提示
            //删除原有
            noticeService.deleteNoticeByItemId(pgPageItem.getId());
            //新增
            PgPageItemNotice notice=pgPageItem.getNotice();
            if (notice!=null) {
                noticeService.insertPgPageItemNotice(notice);
            }
        }else if(pgPageItem.getItemCode().intValue() == 4 ){ //热区切图
            //删除原有
            hotspotareaService.deleteHotspotareaByItemId(pgPageItem.getId());
            //新增
            List<PgPageItemHotspotarea> hotspotareaList=pgPageItem.getHotspotareaList();
            if (hotspotareaList.size()>0) {
                for (int i = 0; i <  hotspotareaList.size() ; i++) {
                    hotspotareaList.get(i).setSort(i+1);
                }
                hotspotareaService.batchSaveHotspotarea(hotspotareaList);
            }
        }else if(pgPageItem.getItemCode().intValue() == 5 ){ //商品展示
            //删除原有
            itemGoodsService.deletePgPageItemGoodsByItemId(pgPageItem.getId());
            //删除模块关联商品列表
            itemGoodsService.deletePgPageItemGoodsListByItemId(pgPageItem.getId());
            //新增
            PgPageItemGoods itemGoods=pgPageItem.getItemGoods();
            if (itemGoods!=null) {
                itemGoodsService.insertPgPageItemGoods(itemGoods);
            }
            //新增商品列表
            List<GdGoods> itemGoodsList = pgPageItem.getItemGoodsList();
            if(itemGoodsList!=null && itemGoodsList.size()>0){
                for (GdGoods gdGoods : itemGoodsList) {
                    gdGoods.setItemId(pgPageItem.getId());
                }
                itemGoodsService.batchInsertPgPageItemGoodsList(itemGoodsList);
            }
        }else if(pgPageItem.getItemCode().intValue() == 6 ){ //视频展示
            //删除原有
            videoService.deletePgPageItemVideoByItemId(pgPageItem.getId());
            //新增
            PgPageItemVideo video=pgPageItem.getVideo();
            if (video!=null) {
                videoService.insertPgPageItemVideo(video);
            }
        }else if(pgPageItem.getItemCode().intValue() == 7 ){ //拼团
            //删除原有
            groupbuyingService.deletePgPageItemGroupbuyingByItemId(pgPageItem.getId());
            //新增
            PgPageItemGroupbuying groupbuy=pgPageItem.getGroupbuy();
            if (groupbuy!=null) {
                groupbuyingService.insertPgPageItemGroupbuying(groupbuy);
            }
        }else if(pgPageItem.getItemCode().intValue() == 8 ){ //限时抢购
            //删除原有
            panicbuyingService.deletePgPageItemPanicbuyingByItemId(pgPageItem.getId());
            //新增
            PgPageItemPanicbuying panicbuy = pgPageItem.getPanicbuy();
            if (panicbuy!=null) {
                panicbuyingService.insertPgPageItemPanicbuying(panicbuy);
            }
        }else if(pgPageItem.getItemCode().intValue() == 9 ){ //单图展示
            //删除原有
            pictureService.deletePgPageItemPictureByItemId(pgPageItem.getId());
            //新增
            PgPageItemPicture picture=pgPageItem.getPicture();
            if (picture!=null) {
                pictureService.insertPgPageItemPicture(picture);
            }
        }
        return pgPageItemMapper.updatePgPageItem(pgPageItem);
    }

    /**
     * 上移
     * @param
     * @return 结果
     */
    @Override
    public int moveUp(PgPageItem pgPageItem) {
        //查询上一个
        PgPageItem q=new PgPageItem();
        q.setPageId(pgPageItem.getPageId());
        q.setSort(pgPageItem.getSort()-1);
        List<PgPageItem> items=pgPageItemMapper.selectPgPageItemList(q);
        if (items.size()>0) {
            pgPageItem.setSort(pgPageItem.getSort()-1);
            this.pgPageItemMapper.updatePgPageItem(pgPageItem);
            PgPageItem item=items.get(0);
            item.setSort(item.getSort()+1);
            this.pgPageItemMapper.updatePgPageItem(item);
        }
        return 1;
    }

    /**
     * 下移
     * @param
     * @return 结果
     */
    @Override
    public int moveDown(PgPageItem pgPageItem) {
        //查询下一个
        PgPageItem q=new PgPageItem();
        q.setPageId(pgPageItem.getPageId());
        q.setSort(pgPageItem.getSort()+1);
        List<PgPageItem> items=pgPageItemMapper.selectPgPageItemList(q);
        if (items.size()>0) {
            pgPageItem.setSort(pgPageItem.getSort()+1);
            this.pgPageItemMapper.updatePgPageItem(pgPageItem);
            PgPageItem item=items.get(0);
            item.setSort(item.getSort()-1);
            this.pgPageItemMapper.updatePgPageItem(item);
        }
        return 1;
    }

    /**
     * 删除页面模块信息
     *
     * @param
     * @return 结果
     */
    @Override
    public int deletePgPageItem(PgPageItem pgPageItem) {
        //删除后后面的模块前移
        this.pgPageItemMapper.batchMoveUp(pgPageItem.getPageId(),pgPageItem.getSort());
        //删除模块详情
        if(pgPageItem.getItemCode().intValue() == 1){
            scrollposterService.deleteScrollposterByItemId(pgPageItem.getId());
        }else if(pgPageItem.getItemCode().intValue() == 3){
            noticeService.deleteNoticeByItemId(pgPageItem.getId());
        }else if(pgPageItem.getItemCode().intValue() == 4){
            hotspotareaService.deleteHotspotareaByItemId(pgPageItem.getId());
        }else if(pgPageItem.getItemCode().intValue() == 5){
            //删除原有
            itemGoodsService.deletePgPageItemGoodsByItemId(pgPageItem.getId());
            //删除模块关联商品列表
            itemGoodsService.deletePgPageItemGoodsListByItemId(pgPageItem.getId());
        }else if(pgPageItem.getItemCode().intValue() == 6){
            videoService.deletePgPageItemVideoByItemId(pgPageItem.getId());
        }else if(pgPageItem.getItemCode().intValue() == 7){
            groupbuyingService.deletePgPageItemGroupbuyingByItemId(pgPageItem.getId());
        }else if(pgPageItem.getItemCode().intValue() == 8){
            panicbuyingService.deletePgPageItemPanicbuyingByItemId(pgPageItem.getId());
        }else if(pgPageItem.getItemCode().intValue() == 9){
            pictureService.deletePgPageItemPictureByItemId(pgPageItem.getId());
        }
        return this.pgPageItemMapper.deletePgPageItemById(pgPageItem.getId());
    }
    /**
     * 根据页面id删除
     *
     * @param
     * @return 结果
     */
    @Override
    public int deletePgPageItemByPageId(Long pageId) {
        return this.pgPageItemMapper.deletePgPageItemByPageId(pageId);
    }


}
